package com.example.item01.service.impl;

import com.example.item01.mapper.EmpMapper;
import com.example.item01.proj.Emp;
import com.example.item01.proj.PageBean;
import com.example.item01.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
//
//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        Integer count = empMapper.count();
//
//        Integer start = (page - 1) * pageSize;//第几行开始
//        List<Emp> empList = empMapper.page(start, pageSize);
//
//        return new PageBean(start,empList);
//
//

    @Override
    public PageBean page(Integer page, Integer pageSize,
                         String name, Short gender, LocalDate begin, LocalDate end) {

        PageHelper.startPage(page, pageSize);

        List<Emp> listed = empMapper.list(name, gender, begin, end);

        Page<Emp> p = (Page<Emp>) listed;

        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.insert(emp);


    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {

        return empMapper.getBynameAndBypassword(emp);

    }
}
















